package cate.game.role.farm.spicefb;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDOperation;
import cate.common.table.farm.fbchallenge.row.FbChallengeRow;
import cate.common.table.farm.spicefb.row.SpiceFbLevelRow;
import cate.game.common.FastOpaqueStorage;
import cate.game.event.spicefb.SpiceFbChallengedEvent;
import cate.game.event.spicefb.SpiceFbChapterUnLockedEvent;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.farm.FightChallengeHandler;
import cate.game.role.farm.spicefb.msg.SpiceFbChallengeResultResp;
import cate.game.role.farm.spicefb.msg.SpiceFbChapterResp;
import cate.game.role.farm.spicefb.po.SpiceFbChapter;
import cate.game.role.farm.spicefb.po.SpiceFbLevel;

import java.util.ArrayList;
import java.util.List;

public class SpiceFbFTC extends AbstractFarmFTC<SpiceFb> {

    /**
     * 当前挑战关卡的模板
     */
    private final SpiceFbLevelRow row;
    private boolean useBuyTimes;

    public SpiceFbFTC(Role role, SpiceFbLevelRow row, boolean useBuyTimes) {
        setup(role);
        this.row = row;
        this.useBuyTimes = useBuyTimes;
    }

    @Override
    protected SpiceFb po(Role role) {
        return role.getFarm().spiceFb;
    }

    @Override
    public void handleResult(Role role, PlayPO play) {
        super.handleResult(role, play);
        boolean win = play.winner == GDFight.Team.A;

        SpiceFb spiceFb = po(role);
        // 战斗结果
        SpiceFbChallengeResultResp resultResp = new SpiceFbChallengeResultResp();
        resultResp.win = win;
        if (win) {
            SpiceFbChapter chapter = spiceFb.getChapter(row.chapter);
            SpiceFbLevel level = chapter.getLevel(row.level);
            level.pass = true;
            if (level.challenge.isEmpty()) {
                spiceFb.todayPassed++;
                if (useBuyTimes) {
                    spiceFb.remainBuyTimes--;
                }
            }
            // 本次战斗有没有完成新的挑战
            boolean challengeChange = false;
            if (level.challenge.size() < row.challengeIds.size()) {
                // 这里要检测战斗挑战的完成情况
                List<Integer> nowDone = new ArrayList<>();
                for (int challengeId : row.challengeIds) {
                    FbChallengeRow challengeRow = role.getGame().table.fbChallenge.spiceFb.get(challengeId);
                    if (challengeRow == null) {
                        continue;
                    }
                    boolean done = FightChallengeHandler.checkDone(role.getGame(), play, challengeRow);
                    if (done) {
                        nowDone.add(challengeId);
                    }
                }
                if (nowDone.size() >= level.challenge.size()) {
                    // 只检测之前没有完成的挑战
                    for (int challengeId : nowDone) {
                        if (!level.challenge.contains(challengeId)) {
                            challengeChange = true;
                        }
                    }
                }
                if (challengeChange) {
                    level.challenge = nowDone;
                }
            }
            resultResp.challenge = level.challenge;
            // 首通
            boolean firstPass = false;
            if (spiceFb.combatNumPass < row.level) {
                firstPass = true;
                spiceFb.combatNumPass = row.level;
            }
            // 发奖励
            MixRes reward = null;
            if (firstPass) {
                role.log.setOperation(GDOperation.SPICE_FB_CHALLENGE);
                reward = new MixRes(row.firstRewardStr);
                reward.add(role, true, GDOperation.SPICE_FB_CHALLENGE);
                role.log.clearOperation();
            }
            resultResp.reward = reward;
            if (challengeChange) {
                try (FastOpaqueStorage ignored = new FastOpaqueStorage(new SpiceFbChapterUnLockedEvent(role))) {
                    // 检查有没有解锁下一章
                    spiceFb.checkUnLock();
                } catch (Throwable ignored) {
                }
                // 章节的数据更新
                chapter.updateChapter();

                role.sendNow(new SpiceFbChapterResp(chapter));
            }
        }
        resultResp.chapterOpen = spiceFb.chapterOpen;
        resultResp.combatNumPass = spiceFb.combatNumPass;
        resultResp.fighting = spiceFb.fighting;
        resultResp.todayPassed = spiceFb.todayPassed;
        resultResp.todayBuyTimes = spiceFb.todayBuyTimes;
        resultResp.remainBuyTimes = spiceFb.remainBuyTimes;
        role.getHistory().action.spiceFbStar(spiceFb.getStar());

        role.sendNow(resultResp);

        publishEvent(role, new SpiceFbChallengedEvent(role, row, resultResp));
        SpiceFbChapterUnLockedEvent.current()
                .filter(SpiceFbChapterUnLockedEvent::isUnlocked)
                .map(event -> event.setResp(resultResp))
                .ifPresent(event -> publishEvent(role, event));

    }
}
